/*
 * @features: 功能
 * @description: 说明
 * @Date: 2020-08-17 10:50:21
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-09-12 15:49:50
 * @LastEditors: judu233
 */

/**战斗事件 */
export enum EEventName {
    /**流程部分 */
    /**战斗开始 */
    FightStart = `FightStart`,
    /**战斗结束 */
    FightOver = `FightOver`,
    /**回合开始 */
    RoundStart = `RoundStart`,
    /**回合结束 */
    RoundOver = `RoundOver`,

    /**战斗控制部分 */
    /**触发玩家战斗 */
    PlayerAttack = `PlayerAttack`,
    /**触发玩家技能 */
    PlayerSkill = `PlayerSkill`,

    /**属性监听 */
    /**触发玩家卡牌血量增减 */
    PlayerHpChange = `PlayerHpChange`,
    /**玩家卡牌死亡 */
    PlayerCardDeath = `PlayerCardDeath`,
    /**玩家卡牌复活 */
    PlayerCardResurrection = `PlayerCardResurrection`,

    /**触发电脑敌人卡牌血量增减 */
    ComputerHpChange = `ComputerHpChange`,
    /**敌方卡牌死亡 */
    ComputerCardDeath = `ComputerCardDeath`,
    /**敌方卡牌复活 */
    ComputerCardResurrection = `ComputerCardResurrection`,

    /**技能·buff */

}



class OnFire {
    es = {};

    on(eventName, cb, once) {
        if (once === void 0) {
            once = false;
        }

        if (!this.es[eventName]) {
            this.es[eventName] = [];
        }

        this.es[eventName].push({
            cb: cb,
            once: once
        });
    }

    once(eventName, cb) {
        this.on(eventName, cb, true);
    };

    fire(eventName, ...param: any[]) {
        let params = [];

        for (let _i = 1; _i < param.length; _i++) {
            params[_i - 1] = param[_i];
        }

        let listeners = this.es[eventName] || [];
        let l = listeners.length;

        for (let i = 0; i < l; i++) {
            let _a = listeners[i],
                cb = _a.cb,
                once = _a.once;
            cb.apply(this, params);

            if (once) {
                listeners.splice(i, 1);
                i--;
                l--;
            }
        }
    };

    off(eventName, cb) {
        // clean all
        if (eventName === undefined) {
            this.es = {};
        } else {
            if (cb === undefined) {
                // clean the eventName's listeners
                delete this.es[eventName];
            } else {
                let listeners = this.es[eventName] || []; // clean the event and listener
                let l = listeners.length;
                for (let i = 0; i < l; i++) {
                    if (listeners[i].cb === cb) {
                        listeners.splice(i, 1);
                        i--;
                        l--;
                    }
                }
            }
        }
    }; // cname of fire


    emit(eventName, ...param: any[]) {
        let params = [];

        for (let _i = 1; _i < param.length; _i++) {
            params[_i - 1] = param[_i];
        }

        this.fire.apply(this, [eventName].concat(params));
    };

    ver = "2.0.1";
}
/**事件管理 */
export let EM: OnFire = new OnFire();